package com.example.easycharge.service;

import com.alibaba.fastjson.JSONObject;
import com.example.easycharge.entity.Vehicle;
import com.example.easycharge.entity.Bill;
import com.example.easycharge.repository.BillRepository;
import com.example.easycharge.repository.VehicleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class BillService {

    @Autowired
    private VehicleRepository vehicleRepository;
    @Autowired
    private BillRepository billRepository;

    // 峰值电价
    private final double PEAK_PRICE = 1.0;
    // 谷值电价
    private final double BOTTOM_PRICE = 0.4;
    // 正常电价
    private final double NORMAL_PRICE = 0.7;
    // 服务费
    private final double SERVICE_PRICE = 0.8;
    //  谷峰区间
    private final String SECTION = "0000000" + "111" + "22222" + "111" + "222" + "11" + "0";
    private double overtimeMoneyPreHour = 1.2;
    private final HashMap<Long, Bill> billMap = new HashMap<>();

    private int tempNowHour = 6;

    public void startBill(long vehicleId, long pileId, int chargePower) {
        Vehicle vehicle = vehicleRepository.findVehicleByVehicleId(vehicleId);
        long customerId = vehicle.getCustomerId();
        Bill bill = new Bill(vehicleId, customerId, pileId, chargePower);
        billMap.put(vehicleId, bill);
    }

    // 每分钟执行一次
    public void addChargeBill(long vehicleId, int peak) {
        Bill bill = billMap.get(vehicleId);
        double chargePower = bill.getChargePower();
        double preBill;
        if (peak == 0) {
            preBill = BOTTOM_PRICE;
        } else if (peak == 1) {
            preBill = PEAK_PRICE;
        } else {
            preBill = NORMAL_PRICE;
        }
        // 每度电价格*千瓦时
        double addMoney = preBill * (chargePower / 60);
        double serviceAddMoney = SERVICE_PRICE * (chargePower / 60);
        bill.chargeAdd(addMoney);
        bill.serviceAdd(serviceAddMoney);
        bill.chargeMinutesAdd(1);
    }

    // 开始充电
    public void startCharge(long vehicleId) {
        Bill thisBill = billMap.get(vehicleId);
        thisBill.setCharging(true);
        thisBill.setStartChargeDate(new Date());
    }

    public Bill endCharge(long vehicleId) {
        Bill thisBill = billMap.get(vehicleId);
        thisBill.setCharging(false);
        thisBill.setEndChargeDate(new Date());
        return thisBill;
    }

    public Bill endBill(long vehicleId) {
        Bill thisBill = billMap.get(vehicleId);
        thisBill.setEndDate(new Date());
        billMap.remove(vehicleId);
        billRepository.save(thisBill);
        return thisBill;
    }

    public List<Bill> getAllBillByCustomerId(long customerId) {
        return billRepository.getBillsByCustomerId(customerId);
    }

    public Bill getBill(long vehicleId) {
        return billMap.getOrDefault(vehicleId, null);
    }

    // 用返回值来表示谷峰，2为峰，0为谷，1为平
    public int peakCheck(int nowHour) {
        // 改成固定时间
        nowHour = tempNowHour;
        if (nowHour > 23) {
            return 1;
        }
        /*
            section区间表示每个时刻是峰值还是谷值，
            比如000011112222就表示
            0-3点谷值，
            4-7点平值，
            8-11点峰值，诸如此类
        * */
        return SECTION.charAt(nowHour) - '0';
    }

    public double getOvertimeMoneyPreHour() {
        return overtimeMoneyPreHour;
    }

    public void setOvertimeMoneyPreHour(double overtimeMoneyPreHour) {
        this.overtimeMoneyPreHour = overtimeMoneyPreHour;
    }

    public JSONObject initBill() {
//        billRepository.initBill();
        JSONObject result = new JSONObject();
        tempNowHour = 6;
        result.put("Bill", "已完成初始化");
        return result;
    }

    public void hourAdd() {
        tempNowHour++;
    }
}
